// 配置API接口地址
console.log('env', process.env.NODE_ENV)
window.root = window.location.origin + '/api';
if (process.env.NODE_ENV === 'production') {
    window.root = 'http://47.100.1.150:8080'
}
// 引用axios
import axios from 'axios';

import {Message} from 'element-ui';

import NProgress from 'nprogress';
import 'nprogress/nprogress.css'; //这个样式必须引入

// 简单配置
//NProgress.configure({easing: 'ease', speed: 500, showSpinner: false})


// http request 请求拦截器，有token值则配置上token值
// axios.interceptors.request.use(
// config => {
//     let token = window.sessionStorage.getItem('token');
//     // 每次发送请求之前判断是否存在token，如果存在，则统一在http请求的header都加上token，不用每次请求都手动添加了
//     if (token) {
//         config.headers['x-auth-token'] = token;
//     }
//     return config;
// },
// err => {
//     return Promise.reject(err);
// });

// http response 服务器响应拦截器，这里拦截401错误，并重新跳入登页重新获取token
/*axios.interceptors.response.use(
  response => {
    if(response.headers["x-auth-token"]){
      //设置token
      window.sessionStorage.setItem('token',response.headers["x-auth-token"])
    }
    return response;
  },
  error => {
      if (error.response) {
          switch (error.response.status) {
              case 401:
                  // 这里写清除token的代码
                  router.replace({
                      path: '/login',
                      query: {redirect: router.currentRoute.fullPath}//登录成功后跳入浏览的当前页面
                  })
          }
      }
      return Promise.reject(error.response.data)
  });
*/

// 自定义判断元素类型JS
// function toType(obj) {
//     return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
// }

// 参数过滤函数
// function filterNull(o) {
//     for (var key in o) {
//         if (o[key] === null) {
//             delete o[key];
//         }
//         if (toType(o[key]) === 'string') {
//             o[key] = o[key].trim();
//         } else if (toType(o[key]) === 'object') {
//             o[key] = filterNull(o[key]);
//         } else if (toType(o[key]) === 'array') {
//             o[key] = filterNull(o[key]);
//         }
//     }
//     return o;
// }

function loadBg() {
    let bg = document.createElement('div');
    let style = bg.style;
    style.position = 'fixed';
    style.height = '100%';
    style.width = '100%';
    style.top = 0;
    style.left = 0;
    style.zIndex = '99999';
    // style.background='red'
    document.body.appendChild(bg);
    setTimeout(() => {
        document.body.removeChild(bg);
    }, 1000);
}

//接口处理函数
function apiAxios(method, url, params, success, failure, noLoading, uploadingConfig, isImg) {
    NProgress.remove();
    NProgress.start();
    if (!noLoading) {
        // loadBg();
    }
    var responseParams = {
        method: method,
        url: url,
        data: method === 'POST' || method === 'PUT' ? params : null,
        params: method === 'GET' || method === 'DELETE' ? params : null,
        baseURL: window.root,
        withCredentials: true,
        //timeout:2000
        //headers:window.sessionStorage.getItem('token')
    };
    if (isImg) {
        responseParams.responseType = 'blob';
    }
    if (uploadingConfig) {
        responseParams.onUploadProgress = function (progressEvent) { //原生获取上传进度的事件
            if (progressEvent.lengthComputable) {
                //属性lengthComputable主要表明总共需要完成的工作量和已经完成的工作是否可以被测量
                //如果lengthComputable为false，就获取不到progressEvent.total和progressEvent.loaded
                uploadingConfig(progressEvent);
            }
        }
    }
    axios(responseParams).then(function (res) {
        NProgress.done();
        //设置token
        // if (res.headers['x-auth-token']) {
        //     window.sessionStorage.setItem('token', res.headers['x-auth-token']);
        // }
        // console.log(res)
        if (isImg && res.status === 200) {
            success(res.data);
        } else if (res.data.code === 200) {
            if (success) {
                success(res.data.message);
            }
        } else if (res.data.code === 501) {
            Message.error(res.data.message);
            location.href = '/login';
            failure(res.data.message)
        } else if (res.data.code === 500 || res.data.code === 503 || res.data.code === 502) {
            Message.error(res.data.message);
        } else if (res.data.code === 99) {
            failure('登录超时，请重新登录');
            sessionStorage.clear();
            localStorage.clear();
            window.location.href = location.href.replace(location.hash, '');
        } else if (res.data.code === '404') {
            if (failure) {
                failure('请求接口不存在');
            }
        } else {
            if (failure) {
                failure(res.data.msg);
            } else {
                Message.error('error: ' + JSON.stringify(res.data.msg));
            }
        }
    }).catch(function (err) {
        NProgress.done();
        console.log('api error, HTTP CODE: ' + err);
        if (failure) {
            failure('网络错误');
        } else {
            Message.error('请检查网络设置');
        }
    });
}

// 返回在vue模板中的调用接口
export default {
    get: function (url, params, success, failure, noLoading, isImg) {
        return apiAxios('GET', url, params, success, failure, noLoading, null, isImg);
    },
    post: function (url, params, success, failure, noLoading, uploadingConfig) {
        return apiAxios('POST', url, params, success, failure, noLoading, uploadingConfig);
    },
};
